package com.lhy.framework.beans.factory.support;

import com.lhy.framework.beans.BeansException;
import com.lhy.framework.beans.factory.factory.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

import java.lang.reflect.Constructor;

/**
 * @ClassName CglibInstantionStrategy
 * @Description cglib创建对象
 * @Author lihaiyang
 * @Date 2022/9/8 23:26
 * @Version 1.0
 */
public class CglibInstantiationStrategy implements InstantiationStrategy {

    @Override
    public Object instantitate(String beanName, BeanDefinition beanDefinition, Constructor constructor, Object[] args) throws BeansException {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(beanDefinition.getClazz());
        enhancer.setCallback(new NoOp() {
            @Override
            public int hashCode() {
                return super.hashCode();
            }
        });
        if (null == constructor) {
            return enhancer.create();
        }

        return enhancer.create(constructor.getParameterTypes(), args);
    }

}
